import java.util.HashSet;

public class Leet520 {
    /*
    LCR 186. 文物朝代判断
     */
    public boolean checkDynasty(int[] places) {
        HashSet<Integer> set = new HashSet<>();
        int max = 0;
        int min = 14;
        for (int x: places) {
            //0 说明没有这个朝代 直接跳过
            if (x == 0) continue;
            max = Math.max(max, x);
            min = Math.min(min, x);
            //如果哈希表中已经存在该朝代，说明出现了多个连续的朝代，返回false
            if (set.contains(x)){
                return false;
            }
            set.add(x);
        }
        //最大朝代-最小朝代 < 5说明连续
        return max - min < 5;
    }

/*
LCR 182. 动态口令
 */
    public String dynamicPassword(String password, int target) {
        String a = password.substring(0, target);
        String b = password.substring(target, password.length());
        return b+  a;
    }
/*

LCR 181. 字符串中的单词反转
 */
    public String reverseMessage(String message) {
        //使用正则表达式把中间多个空格替换成一个空格
        String[] strs =  message.trim().replaceAll("\\s+", " ").split(" ");
        //翻转strs
        for (int i = 0; i < strs.length / 2; i++) {
            String temp = strs[i];
            strs[i] = strs[strs.length - 1 - i];
            strs[strs.length - 1 - i] = temp;
        }
        StringBuilder sb = new StringBuilder();
        for (String s: strs) {
            sb.append(s);
            sb.append(" ");
        }
        //sb最后一个空格

        return sb.toString().trim();
    }
class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
/*
LCR 171. 训练计划 V
 */
    ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        ListNode A = headA;
        ListNode B = headB;
        while (A != B){
            //c为公共长度
            //当headA走了a+c步 headB走了b+c步
            //当headA走了a+c+b步 headB走了b+c+a步
            // 步数相同，此时headA和headB相遇
            A = A == null ? headB : A.next;
            B = B == null ? headA : B.next;
        }
        return A;
    }

/*
LCR 179. 查找总价格为目标值的两个商品
 */
    public int[] twoSum(int[] price, int target) {
        HashSet<Integer> set = new HashSet<>();
        for (int x : price) {
            set.add(x);
        }
        for(int i = 0; i < price.length; i++){
            if (set.contains(target - price[i])){
                return new int[]{price[i],(target - price[i])};
            }
        }
        return null;
    }
}
